from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth import logout, login, authenticate
from .forms import LoginForm
from django.shortcuts import render, redirect
from django.http import HttpResponse
from django.contrib.contenttypes.models import ContentType
from .models import Article
from django.contrib.auth.models import Permission, User
from django.urls import reverse
# Create your views here.


def one_view(request):
    user = User.objects.create_user(
        username='zzlb', email='zzzzzz@qq.com', password='111111')
    return HttpResponse('ok')


def my_authenticate(telephone, password):
    user = User.objects.filter(extension__telephone=telephone).first()
    if user:
        is_correct = user.check_password(password)
        if is_correct:
            return user
    return None


def one_post(request):
    telephone = request.GET.get('telephone')
    password = request.GET.get('password')
    user = my_authenticate(telephone, password)
    if user:
        print('验证成功', user)
    else:
        print('未找到用户')


def mylogin(request):
    if request.method == 'GET':
        return render(request, 'login.html')

    form = LoginForm(request.POST)
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        remember = form.cleaned_data.get('remember')
        user = authenticate(request, username=username, password=password)
        if user and user.is_active:
            login(request, user)
            if remember:
                # 记住我，永不过期
                request.session.set_expiry(None)
            else:
                request.session.set_expiry(0)
            next_url = request.GET.get('next')
            if next_url:
                return redirect(next_url)
            return HttpResponse('登陆成功')
        else:
            return HttpResponse('手机号码或者密码错误')
    else:
        print(form.errors)
        return render(request, 'login.html')


@login_required(login_url='/login/')
def profile(request):
    return HttpResponse('个人中心')


def mylogout(request):
    logout(request)
    return HttpResponse('退出成功')


def add_permission(request):
    content_type = ContentType.objects.get_for_model(Article)
    permission = Permission.objects.create(
        codename='black_article', name='拉黑文章', content_type=content_type)
    return HttpResponse('文章创建成功')


def operate_permission(request):
    user = User.objects.first()
    content_type = ContentType.objects.get_for_model(Article)
    permissions = Permission.objects.filter(content_type=content_type)
    for permission in permissions:
        print(permission)
    # user.user_permissions.add(permissions[0])
    user.user_permissions.add(*permissions)
    # user.user_permissions.remove(permissions[0])
    user.user_permissions.clear()
    user.user_permissions.set(permissions)
    if user.has_perm('front.view_article'):
        print('拥有view_article')
    else:
        print('缺少')

    user.has_perm('front.auditarticle')
    return HttpResponse('操作权限')


@permission_required('front.view_article')
def view_article(request, id):
    a = Article.objects.get(pk=id)
    print(a)
    return HttpResponse('success')


def add_article(request):
    if request.user.is_authenticated:
        print('已经登陆了')
        if request.user.has_perm('front.add_article'):
            return HttpResponse('添加文章')
        else:
            return HttpResponse('没有权限')
    else:
        return redirect(reverse('front.login'))


@permission_required('front.add_article', raise_exception=True)
def add_article2(request):
    if request.user.is_authenticated:
        print('已经登陆了')
        if request.user.has_perm('front.add_article'):
            return HttpResponse('添加文章')
        else:
            return HttpResponse('没有权限')
    else:
        return redirect(reverse('front.login'))


def usergroup(request):
    user = User.objects.first()
    perimissions = user.get_group_permissions()
    print(perimissions)
    user.has_perm('front.view_article')
    return HttpResponse('ok')
